package utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import objects.BoardData;
import objects.Card;
import objects.CardType;
import objects.Character;
import objects.Envelope;
import objects.Player;
import objects.Room;
import objects.Weapon;

public class AutoDealer 
{
	/**
	 *  Automatically (and randomly) deals cards to all players and envelope.
	 */
	public static BoardData Deal(BoardData bdToCheck)
	{
		if (bdToCheck.getPlayers() == null || bdToCheck.getPlayers().size() == 0)
		{
			return new BoardData();
		}
		
		int[] arrWeaponIDs = new int[Weapon.values().length];
		int[] arrRoomIDs = new int[Room.values().length];
		int[] arrCharIDs = new int[Character.values().length];
		
		Random rndRandomizer = new Random();
		List<Player> lstPlayers = bdToCheck.getPlayers();
		
		int nWeaponEnv = rndRandomizer.nextInt(arrWeaponIDs.length);
		int nCharEnv = rndRandomizer.nextInt(arrCharIDs.length);
		int nRoomEnv = rndRandomizer.nextInt(arrRoomIDs.length);
		Map <Integer, List<Card>> htCards = new HashMap<Integer, List<Card>>();
		
		Envelope envToAdd = new Envelope();
		
		for (int i=0; i < lstPlayers.size(); i++)
		{
			lstPlayers.get(i).setCardsList(new ArrayList<Card>());
			htCards.put(i, new ArrayList<Card>());
		}
		
		for (int i=0; i < arrWeaponIDs.length / 2; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.WEAPON);
			crdToAdd.setName(Weapon.values()[i].name());
			if (i == nWeaponEnv)
			{
				envToAdd.setWeaponCard(crdToAdd);
			}
			else
			{
				int playerNumber = rndRandomizer.nextInt(lstPlayers.size());
				htCards.get(playerNumber).add(crdToAdd);
			}
		}
		
		for (int i=arrWeaponIDs.length / 2; i < arrWeaponIDs.length; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.WEAPON);
			crdToAdd.setName(Weapon.values()[i].name());
			if (i == nWeaponEnv)
			{
				envToAdd.setWeaponCard(crdToAdd);
			}
			else
			{
				htCards.get(i % lstPlayers.size()).add(crdToAdd);
			}
		}
		
		
		for (int i=0; i < arrRoomIDs.length /2 ; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.ROOM);
			crdToAdd.setName(Room.values()[i].name());
			if (i == nRoomEnv)
			{
				envToAdd.setRoomCard(crdToAdd);
			}
			else
			{
				int playerNumber = rndRandomizer.nextInt(lstPlayers.size());
				htCards.get(playerNumber).add(crdToAdd);
			}
		}
		
		for (int i=arrRoomIDs.length /2; i < arrRoomIDs.length; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.ROOM);
			crdToAdd.setName(Room.values()[i].name());
			if (i == nRoomEnv)
			{
				envToAdd.setRoomCard(crdToAdd);
			}
			else
			{
				htCards.get(i % lstPlayers.size()).add(crdToAdd);
			}
		}
		
		for (int i=0; i < arrCharIDs.length / 2; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.CHARACTER);
			crdToAdd.setName(Character.values()[i].name());
			if (i == nCharEnv)
			{
				envToAdd.setCharacterCard(crdToAdd);
			}
			else
			{
				int playerNumber = rndRandomizer.nextInt(lstPlayers.size());
				htCards.get(playerNumber).add(crdToAdd);
			}
		}
		
		for (int i=arrCharIDs.length/2; i < arrCharIDs.length; i++)
		{
			Card crdToAdd = new Card();
			crdToAdd.setType(CardType.CHARACTER);
			crdToAdd.setName(Character.values()[i].name());
			if (i == nCharEnv)
			{
				envToAdd.setCharacterCard(crdToAdd);
			}
			else
			{
				htCards.get(i % lstPlayers.size()).add(crdToAdd);
			}
		}
				
		for (int i=0; i < lstPlayers.size(); i++)
		{
			lstPlayers.get(i).setCardsList(htCards.get(i));
		}
		
		bdToCheck.setEnvelope(envToAdd);
		bdToCheck.setPlayers(lstPlayers);
		
		return bdToCheck;
	}
}
